package index

import (
	"gitcode.com/G-YT/iHealth25/server/global"
	"gitcode.com/G-YT/iHealth25/server/model/common/response"
	hierarchyReq "gitcode.com/G-YT/iHealth25/server/model/hierarchy/request"
	"gitcode.com/G-YT/iHealth25/server/model/public"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type IndexApi struct {
}

// GetLevelTwoList 获取LevelTwo列表
// @Tags Index
// @Summary 分页获取LevelTwo列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/index/getTwoLevelList [get]
func (i *IndexApi) GetLevelTwoList(c *gin.Context) {
	var ht hierarchyReq.LevelTwoSearch
	err := c.ShouldBindQuery(&ht)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if list, err := levelTwoService.GetTwoLevelListByWhere(ht); err != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(err))
		response.FailWithMessage("获取失败", c)
	} else {
		var ResArr []map[string]interface{}
		for k, l := range list {
			ResArr = append(ResArr, map[string]interface{}{
				"id":    l.ID,
				"name":  l.Name,
				"value": k,
			})
		}
		response.Result(200, ResArr, "获取成功", c)
	}
}

// VerifyDeviceCode 验证设备码
// @Tags Index
// @Summary 验证设备码
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string {"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/index/verifyDeviceCode [get]
func (i *IndexApi) VerifyDeviceCode(c *gin.Context) {
	var ib public.BluetoothDeviceRecord
	err := c.ShouldBindQuery(&ib)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if data, verifyErr := IndexService.VerifyDeviceCode(&ib); verifyErr != nil {
		global.GVA_LOG.Error("获取失败!", zap.Error(verifyErr))
		response.FailWithDetailed(data, verifyErr.Error(), c)
	} else {
		response.Result(200, data, "获取成功", c)
	}
}

// SaveDeviceCode 保存设备码
// @Tags Index
// @Summary 保存设备码
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string {"success":true,"data":{},"msg":"获取成功"}"
// @Router /api/index/saveDeviceCode [post]
func (i *IndexApi) SaveDeviceCode(c *gin.Context) {
	var ib public.BluetoothDeviceRecord
	err := c.ShouldBindJSON(&ib)
	if err != nil {
		response.FailWithMessage(err.Error(), c)
		return
	}
	if saveErr := IndexService.SaveDeviceCode(&ib); saveErr != nil {
		global.GVA_LOG.Error("创建失败!", zap.Error(saveErr))
		response.FailWithMessage(saveErr.Error(), c)
	} else {
		response.OkWithMessage("创建成功", c)
	}
}
